//
//  ILFashionListViewController.m
//  ILife
//
//  Created by lanou on 15/10/27.
//  Copyright © 2015年 ILife. All rights reserved.
//

#import "ILFashionListViewController.h"
#import "ILSelectionTableViewCell.h"
#import <AFHTTPRequestOperationManager.h>
#import <MJRefresh.h>
#import "ILFashionWebViewController.h"
@interface ILFashionListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@property (nonatomic, assign) NSInteger pageNum;

@end

@implementation ILFashionListViewController

- (NSMutableArray *)dataSourceArray
{
    if (_dataSourceArray == nil) {
        self.dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArray;
}





- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tabBarController.tabBar.hidden = NO;
    self.pageNum = 1;
    
    // 取消分割线
    self.mainTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    
    [self reloadTableView];
    [self requestData];
    
// 回到顶部按钮
    UIButton *topButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [topButton setImage:[UIImage imageNamed:@"top"] forState:(UIControlStateNormal)];
    topButton.frame = CGRectMake(SCREEN_WIDTH - 80, SCREEN_HEIGHT - 120, 50, 50);
    topButton.backgroundColor = [UIColor grayColor];
    topButton.alpha = 0.5;
    topButton.layer.cornerRadius = 25;
    topButton.layer.masksToBounds = YES;
    [topButton addTarget:self action:@selector(toTop) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:topButton];
    
    
    // 下拉刷新
    self.mainTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    }];
    
    self.mainTableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshHeader)];
    
    
    // 上提加载
    self.mainTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
    }];
    self.mainTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFooter)];
    [self.mainTableView.footer beginRefreshing];

    
    
}


// 返回顶部
- (void)toTop
{
    [self.mainTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
}


- (void)reloadTableView
{
    _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:(UITableViewStylePlain)];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    
    [_mainTableView registerClass:[ILSelectionTableViewCell class] forCellReuseIdentifier:@"select"];
    [self.view addSubview:_mainTableView];
    
}
- (void)refreshHeader
{
    [self.dataSourceArray removeAllObjects];
    self.pageNum = 1;
    [self requestData];
}

- (void)refreshFooter
{
    self.pageNum +=1;
    [self requestData];
}



- (void)requestData
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    // AFNetWork2.0以上要添加下面的话
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];//设置相应内容类型
    
    NSLog(@"pageNum = %ld",(long)self.pageNum);
    NSString *str = @"http://app.ellemen.com/?app=rss&controller=elleplus&action=elleplus_channellist&num=10&catid=";
    NSString *urlStr = [NSString stringWithFormat:@"%@%@&pagenum=%ld",str,self.catid,(long)self.pageNum];
    NSLog(@"urlStr=%@",urlStr);
    
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        
        
        for (NSDictionary *dic in responseObject[@"data"]) {
            ILHomePageModel *homeModel = [[ILHomePageModel alloc]init];
            [homeModel setValuesForKeysWithDictionary:dic];
            [self.dataSourceArray addObject:homeModel];
        }
        
        
        // 停止刷新
        [self.mainTableView.header endRefreshing];
        [self.mainTableView.footer endRefreshing];

        
        [self.mainTableView reloadData];
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        NSLog(@"error %@",error);
    }];
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_WIDTH + 40;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ILSelectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"select" forIndexPath:indexPath];
    
    cell.configuer = _dataSourceArray[indexPath.row];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ILFashionWebViewController *webVC = [[ILFashionWebViewController alloc]init];
    webVC.webUrl =((ILHomePageModel *) _dataSourceArray[indexPath.row]).url;
    webVC.contentid = ((ILHomePageModel *) _dataSourceArray[indexPath.row]).contentid;
    webVC.home_contentid = @"0";
    webVC.from = @"2";
    
    [self.navigationController pushViewController:webVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
